<template>
  <div class="w h over-h">
    <div class="category-title a-i-c f-s dialog-page-title">
      <el-button type='text' icon="ArrowLeft" @click="goBackToPage">返回</el-button>
      <div class="m-l10">
        {{ params.isEdit ? '角色编辑' : '角色新增' }}
      </div>
    </div>
    <div class="w f-c a-i-c" :style="normalBodyHeight">
      <div style="width:600px">
        <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h" :layout="editFromLayout"
          v-model="editForm">
          <template #meta>
            <span class="c7">
              Meta:{{ metaString }}
            </span>
          </template>
        </lm-form>
        <div class="lm-w lm-h lm-f-s">
          <div class="lm-w-120 lm-h"></div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="reset" plain>重置</el-button>
          <el-button type="primary" @click="goBackToPage()" plain>返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { Role, type RoleInfo } from '@/apis/role/index'
import { RequestCode } from '@/server/base'
import { ValidateFieldsError } from 'async-validator'

export default {
  name: 'RoleEdit',
  mixins: [frameProperty],
  props: {
    goBackToPage: Function,
    params: [Object, Array]
  } as any,
  data() {
    return {
      editFormRefKey: 'roleModifyRef',
      menuTree: [],
      editForm: {} as {} | RoleInfo | any,
      editFromLayout: [
        {
          prop: 'name',
          label: '角色名称',
          type: 'input',
          itemClass: 'column_1',
          message: '请输入2-20个字符',
          required: true,
          clearable: true,
          regex: /^.{0,20}$/
        },
        // {
        //   prop: 'code',
        //   label: 'Code',
        //   type: 'input',
        //   itemClass: 'column_1',
        //   message: '请输入2-20个字符',
        //   // required: true,
        //   clearable: true,
        //   regex: /^.{0,20}$/
        // } 
      ] as any[]
    }
  },
  computed: {
    modifyInfo() {
      let row = this.params.row || {}
      return row
    }
  },
  methods: {
    getDataList() { },
    getDetailById() {
      if (!this.modifyInfo) {
        return
      }
      this.editForm = this.modifyInfo
    },
    saveRoleInfo() {
      const { label, customLabel, hidden, width, handler, elIcon, ...params } = this.editForm
      params.meta = this.metaString
      const method = this.params.isEdit ? Role.modifyRoleInfo : Role.addRoleInfo
      method(params)
        .then((res: RequestCode) => {
          this.showMessage({ type: 'success', message: '操作成功！' })
          this.goBackToPage(true)
        })
        .catch((error: any) => { })
    },
    //验证数据
    submit() {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.saveRoleInfo()
      })
    },
    //重置
    reset() {
      this.editForm = {}
    },

    

  },
  created() {
    this.$nextTick(() => {
      this.getDetailById()
      this.getDataList()
    })
  }
}
</script>

<style scoped></style>